package com.itheima.admin.listener;


import com.itheima.admin.service.WemediaNewsAutoScanService;
import com.itheima.common.constants.BusinessConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Component
@Slf4j
public class WemediaNewsAutoScanListener {

    @Autowired
    private WemediaNewsAutoScanService wemediaNewsAutoScanService;

    @KafkaListener(topics = BusinessConstants.MqConstants.WM_NEWS_AUTO_SCAN_TOPIC)
    public void autoScanWemediaNews(ConsumerRecord<String, String> msgRecord) {
        String uuid = UUID.randomUUID().toString();
        log.debug("自动审核开始:uuid={},id={},t={},p={}", uuid, msgRecord.value(), msgRecord.topic(), msgRecord.partition());

        try {
            wemediaNewsAutoScanService.autoScanWemediaNewsById(Integer.valueOf(msgRecord.value()), uuid);
        } catch (Exception e) {
            log.error("===================" + e.getMessage());
            log.error("【出错】文章自动审核：uuid={},id={},t={},p={}", uuid, msgRecord.value(), msgRecord.topic(), msgRecord.partition());
        }
        log.info("自动审核结束：uuid={},id={},t={},p={}", uuid, msgRecord.value(), msgRecord.topic(), msgRecord.partition());
    }
}
